﻿using Fidely.Framework;
using Fidely.Framework.Compilation.Objects.Evaluators;
using Fidely.Framework.Compilation.Objects.Operators;
using Fidely.Framework.Compilation.Operators;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.ViewModels.Events;
using System;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.ViewModels
{
    public class CodeInspectionResultViewModel : CodeAnalysisResultViewModelBase<ProblemViewModel>
    {
        public event EventHandler<ShowRuleRequiredEventArgs> ShowRuleRequired;


        private static CodeInspectionResultGridViewModel grid = new CodeInspectionResultGridViewModel();


        public CodeInspectionResultGridViewModel Grid
        {
            get
            {
                return grid;
            }
        }


        public CodeInspectionResultViewModel()
            : base(CreateCompilerSetting())
        {
        }


        public void ShowRule(ProblemViewModel problem)
        {
            OnShowRuleRequired(new ShowRuleRequiredEventArgs(problem));
        }

        protected virtual void OnShowRuleRequired(ShowRuleRequiredEventArgs e)
        {
            if (ShowRuleRequired != null)
            {
                ShowRuleRequired(this, e);
            }
        }

        private static CompilerSetting CreateCompilerSetting()
        {
            var setting = new CompilerSetting();

            setting.Evaluators.Add(new PropertyEvaluator<ProblemViewModel>());
            setting.Evaluators.Add(new TypeConversionEvaluator());

            setting.Operators.Add(new PartialMatch<ProblemViewModel>(":", true, OperatorIndependency.Strong, "Partial matching operator"));
            setting.Operators.Add(new PrefixSearch<ProblemViewModel>("=:", true, OperatorIndependency.Strong, "Prefix search operator"));
            setting.Operators.Add(new SuffixSearch<ProblemViewModel>(":=", true, OperatorIndependency.Strong, "Suffix search operator"));
            setting.Operators.Add(new Equal<ProblemViewModel>("=", true, OperatorIndependency.Strong, "Equal operator"));

            return setting;
        }
    }
}
